/**
 * 
 */
package com.youyun56.gps.android;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SlidingDrawer;
import android.widget.Spinner;
import android.widget.Toast;

import com.youyun56.gps.android.commands.CommandType;
import com.youyun56.gps.android.common.BaseActovity;
import com.youyun56.gps.android.common.CommandFactory;
import com.youyun56.gps.android.common.JSONKey;
import com.youyun56.gps.android.events.GoodInfoItemClickListerner;
import com.youyun56.gps.android.events.GoodInfoMenuItemClick;
import com.youyun56.gps.android.events.GoodInfoScrollListener;
import com.youyun56.gps.android.http.HttpManager;
import com.youyun56.gps.android.http.IHttpListener;
import com.youyun56.gps.android.pojo.Car;
import com.youyun56.gps.android.pojo.City;
import com.youyun56.gps.android.pojo.GoodInfo;
import com.youyun56.gps.android.pojo.Province;
import com.youyun56.gps.android.pojo.SearchObject;
import com.youyun56.gps.android.pool.GoodInfoHander;
import com.youyun56.gps.android.service.LocationUploadService;

/**
 * @author Administrator
 */
public class GoodInfoActivity extends BaseActovity {
    private Spinner                        provinceSpiner;
    private Spinner                        citySpiner;
    private AlertDialog                    menuDialog;                                                                                                         // menu菜单Dialog
    private GridView                       menuGrid;
    private View                           menuView;
    private final static String            tag                      = GoodInfoActivity.class.getName();
    private final static List<Province>    provinces                = new ArrayList<Province>();
    private final static List<City>        citys                    = new ArrayList<City>();
    private Hashtable<Integer, List<City>> provinceCity             = new Hashtable<Integer, List<City>>();
    private final static String            provinceFile             = "province.xml";
    private final static String            cityFile                 = "city.xml";
    private ArrayAdapter<City>             cityAdapter;
    public static GoodInfoHander           handler;
    private GoodInfoItemAdapter            goodInfoAdapter;
    private GoodInfoItemAdapter            searchGoodInfoAdapter;
    private int[]                          menu_image_array         = {R.drawable.icon_4_n, android.R.drawable.ic_delete, android.R.drawable.ic_input_delete };
    private int[]                          menu_name_array          = {R.string.searchgood_goodSearch, R.string.sys_out, R.string.back };
    private ListView                       listView;
    private ListView                       searchListView;
    private AlertDialog                    searchDialog;
    private SearchObject                   so;
    private View                           footerView;
    private View                           searchInfoView;
    private ScheduledExecutorService       scheduledExecutorService = Executors.newScheduledThreadPool(1);
    private SlidingDrawer                  slidingDrawer;
    private Spinner                        fromProvince;
    private Spinner                        toProvince;
    private Spinner                        fromCity;
    private Spinner                        toCity;
    private ArrayAdapter<City>             fromCityAdapter;
    private ArrayAdapter<City>             toCityAdapter;
    
    private Button                         btnExpand;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.goodinfo);
        init();
        fillProvince();
        fillCitys();
        fillCitySpinner();
        fillProvinceSpinner();
        focusProvince();
        focusCity();
        initListView();
        initSearchListView();
        initMenu();
        Toast.makeText(this, "您可以点击\"菜单键\"对货物进行搜索", Toast.LENGTH_LONG).show();
        handler = new GoodInfoHander(this);
        View layout = this.findViewById(R.id.targetCity);
        layout.setVisibility(View.GONE);
        scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
            
            @Override
            public void run() {
                try {
                    if (MainTabActivity.service != null) {
                        GoodInfo goodInfo = goodInfoAdapter.getFirst();
                        int lastindex = -1;
                        if (goodInfo != null) {
                            lastindex = goodInfo.getIndex();
                        }
                        final int lastIndex = lastindex;
                        HttpManager.getInstance().excutePost(CommandFactory.getAppendGoods(((City) getCitySpiner().getSelectedItem()).getId(), lastindex), new IHttpListener() {
                            
                            @Override
                            public void onBack(boolean bSuccess, String strData, int nResponseCode) {
                                // TODO Auto-generated method stub
                                if (bSuccess) {
                                    try {
                                        Message msg = handler.obtainMessage();
                                        if (lastIndex == -1) {
                                            msg.what = CommandType.GetGoods;
                                        } else {
                                            msg.what = CommandType.Append_Good;
                                        }
                                        msg.obj = new JSONObject(strData);
                                        msg.sendToTarget();
                                    } catch (JSONException e) {
                                        // TODO Auto-generated catch
                                        // block
                                        e.printStackTrace();
                                    }
                                }
                            }
                        });
                    }
                    
                } catch (Exception e) {
                    // TODO: handle exception
                    Log.e(tag, e.getMessage(), e);
                }
            }
        }, 0, 5, TimeUnit.SECONDS);
        
        // alarmManager = (AlarmManager)
        // getSystemService(Context.ALARM_SERVICE);
        // Intent intent = new Intent(this, UploadLocationService.class);
        // intent.putExtras(Intent);
        // pendingIntent = PendingIntent.getBroadcast(this, 0, intent,
        // PendingIntent.FLAG_CANCEL_CURRENT);
        // alarms.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        // SystemClock.elapsedRealtime(), this.uploadInterval * 1000,
        // pendingIntent);
        initSpiderBar();
    }
    
    private void initSpiderBar() {
        // TODO Auto-generated method stub
        slidingDrawer = (SlidingDrawer) findViewById(R.id.sliding);
        // slidingDrawer.setOnDrawerOpenListener(new
        // SlidingDrawer.OnDrawerOpenListener()// 开抽屉
        // {
        // @Override
        // public void onDrawerOpened() {
        // iv.setImageResource(R.drawable.touch_handler);// 响应开抽屉事件
        // // ，把图片设为向下的
        // }
        // });
        // slidingDrawer.setOnDrawerCloseListener(new
        // SlidingDrawer.OnDrawerCloseListener() {
        // @Override
        // public void onDrawerClosed() {
        // iv.setImageResource(R.drawable.touch_handler);// 响应关抽屉事件
        // }
        // });
    }
    
    private void initMenu() {
        // TODO Auto-generated method stub
        menuView = View.inflate(this, R.layout.gridview_menu, null);
        menuDialog = new AlertDialog.Builder(this).create();
        menuDialog.setView(menuView);
        menuGrid = (GridView) menuView.findViewById(R.id.gridview);
        menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array));
        menuGrid.setOnItemClickListener(new GoodInfoMenuItemClick(this));
        // menuDialog.show();
    }
    
    private SimpleAdapter getMenuAdapter(int[] menu_name_array, int[] menu_image_array) {
        ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < menu_name_array.length; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("itemImage", menu_image_array[i]);
            map.put("itemText", getResources().getString(menu_name_array[i]));
            data.add(map);
        }
        SimpleAdapter simperAdapter = new SimpleAdapter(this, data, R.layout.item_menu, new String[] {"itemImage", "itemText" }, new int[] {R.id.item_image, R.id.item_text });
        return simperAdapter;
    }
    
    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        menuDialog.show();
        return false;
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add("");
        return super.onCreateOptionsMenu(menu);
    }
    
    private void fillCitys() {
        // TODO Auto-generated method stub
        try {
            DocumentBuilderFactory docBuilderFactory = null;
            DocumentBuilder docBuilder = null;
            Document doc = null;
            docBuilderFactory = DocumentBuilderFactory.newInstance();
            docBuilder = docBuilderFactory.newDocumentBuilder();
            // xml file 放到 assets目录中的
            doc = docBuilder.parse(getResources().getAssets().open(cityFile));
            // root element
            Element root = doc.getDocumentElement();
            NodeList rows = root.getElementsByTagName("row");
            for (int i = 0; i < rows.getLength(); i++) {
                City city = new City();
                NodeList children = rows.item(i).getChildNodes();
                city.setId(Integer.parseInt(children.item(0).getFirstChild().getNodeValue()));
                city.setName(children.item(1).getFirstChild().getNodeValue());
                Integer provinceId = Integer.parseInt(children.item(2).getFirstChild().getNodeValue());
                if (!provinceCity.containsKey(provinceId)) {
                    provinceCity.put(provinceId, new ArrayList<City>());
                }
                provinceCity.get(provinceId).add(city);
            }
        } catch (Exception e) {
            Log.e(tag, e.getMessage(), e);
        }
    }
    
    private void fillProvince() {
        
        try {
            DocumentBuilderFactory docBuilderFactory = null;
            DocumentBuilder docBuilder = null;
            Document doc = null;
            docBuilderFactory = DocumentBuilderFactory.newInstance();
            docBuilder = docBuilderFactory.newDocumentBuilder();
            // xml file 放到 assets目录中的
            doc = docBuilder.parse(getResources().getAssets().open(provinceFile));
            // root element
            Element root = doc.getDocumentElement();
            NodeList rows = root.getElementsByTagName("row");
            for (int i = 0; i < rows.getLength(); i++) {
                Province province = new Province();
                Node node = rows.item(i);
                province.setId(Integer.parseInt(node.getFirstChild().getFirstChild().getNodeValue()));
                province.setName(node.getLastChild().getFirstChild().getNodeValue());
                provinces.add(province);
            }
        } catch (Exception e) {
            Log.e(tag, e.getMessage(), e);
        }
    }
    
    private void init() {
        this.provinceSpiner = (Spinner) findViewById(R.id.province);
        this.citySpiner = (Spinner) findViewById(R.id.city);
    }
    
    private void initListView() {
        // TODO Auto-generated method stub
        listView = (ListView) findViewById(R.id.goodInfoList);
        // listView.setOnItemSelectedListener(new
        // GoodInfoItemSelectedListener(this));
        listView.setOnItemClickListener(new GoodInfoItemClickListerner(this));
        goodInfoAdapter = new GoodInfoItemAdapter(this);
        footerView = View.inflate(this, R.layout.loadmore, null);
        listView.addFooterView(footerView);
        listView.setAdapter(goodInfoAdapter);
        Button btnLoadMore = (Button) footerView.findViewById(R.id.loadMore);
        btnLoadMore.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                GoodInfo goodInfo = goodInfoAdapter.getLast();
                int lastindex = -1;
                if (goodInfo != null) {
                    lastindex = goodInfo.getIndex();
                }
                HttpManager.getInstance().excutePost(CommandFactory.getAppendGoodsEnd(((City) getCitySpiner().getSelectedItem()).getId(), lastindex), new IHttpListener() {
                    
                    @Override
                    public void onBack(boolean bSuccess, String strData, int nResponseCode) {
                        // TODO Auto-generated method stub
                        if (bSuccess) {
                            try {
                                Message msg = handler.obtainMessage();
                                msg.what = CommandType.Append_Good_END;
                                msg.obj = new JSONObject(strData);
                                msg.sendToTarget();
                            } catch (JSONException e) {
                                // TODO Auto-generated catch
                                Log.e(tag, e.getMessage(), e);
                            }
                        }
                    }
                });
            }
        });
    }
    
    private void initSearchListView() {
        // TODO Auto-generated method stub
        // init searchPanel
        // end init searchPanel
        
        searchListView = (ListView) findViewById(R.id.searchInfoList);
        searchListView.setOnItemClickListener(new GoodInfoItemClickListerner(this));
        searchGoodInfoAdapter = new GoodInfoItemAdapter(this);
        searchInfoView = View.inflate(this, R.layout.search_good_view, null);
        searchListView.addHeaderView(searchInfoView);
        initComponent();
        fillDate();
        bindEvents();
        searchListView.setAdapter(searchGoodInfoAdapter);
        btnExpand = (Button) findViewById(R.id.btnExpand);
        btnExpand.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                changeSearchPanelVisibility();
            }
        });
        findViewById(R.id.btnSearch).setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                City from = (City) fromCity.getSelectedItem();
                City to = (City) toCity.getSelectedItem();
                City targetCity = (City) GoodInfoActivity.this.getCitySpiner().getSelectedItem();
                SearchObject so = new SearchObject();
                so.setTargetCity(targetCity.getId());
                if (from == null || from.getId() == null) {
                    Toast.makeText(GoodInfoActivity.this, "出发城市不能都为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (from != null) {
                    so.setFromCity(from.getId() != null ? from.getId() : -1);
                }
                if (to != null) {
                    so.setToCity(to.getId() != null ? to.getId() : -1);
                }
                GoodInfoActivity.this.getSearchGoodInfoAdapter().removeAll();
                GoodInfoActivity.this.setSo(so);
                LinearLayout searchPanel = (LinearLayout) findViewById(R.id.searchPanel);
                if (View.VISIBLE == searchPanel.getVisibility()) {
                    searchPanel.setVisibility(View.GONE);
                    btnExpand.setText(R.string.expand);
                }
                HttpManager.getInstance().excutePost(CommandFactory.searchGood(so), new IHttpListener() {
                    
                    @Override
                    public void onBack(boolean bSuccess, String strData, int nResponseCode) {
                        // TODO Auto-generated method stub
                        try {
                            JSONObject input = new JSONObject(strData);
                            JSONArray searchGoods = input.getJSONArray(JSONKey.GOODS);
                            for (int i = 0; i < searchGoods.length(); i++) {
                                JSONObject o = (JSONObject) searchGoods.get(i);
                                GoodInfo goodInfo = wrapGoodInfo(o);
                                getSearchGoodInfoAdapter().addLast(goodInfo);
                            }
                            getSearchListView().setOnScrollListener(GoodInfoScrollListener.instance);
                        } catch (Exception e) {
                            // TODO: handle exception
                            Log.e(tag, e.getMessage(), e);
                        }
                    }
                });
            }
        });
    }
    
    private void bindEvents() {
        fromProvince.setOnItemSelectedListener(new OnItemSelectedListener() {
            
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                Province province = (Province) fromProvince.getSelectedItem();
                fromCityAdapter.clear();
                if (province.getId() == null) {
                    return;
                }
                fromCityAdapter.add(new City(GoodInfoActivity.this.getResources().getString(R.string.any)));
                List<City> citys = GoodInfoActivity.this.getProvinceCity().get(province.getId());
                for (City city : citys) {
                    fromCityAdapter.add(city);
                }
                // fromCity.setSelection(0);
                fromCityAdapter.notifyDataSetChanged();
            }
            
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                
            }
        });
        
        toProvince.setOnItemSelectedListener(new OnItemSelectedListener() {
            
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                Province province = (Province) toProvince.getSelectedItem();
                toCityAdapter.clear();
                if (province.getId() == null) {
                    return;
                }
                toCityAdapter.add(new City(GoodInfoActivity.this.getResources().getString(R.string.any)));
                List<City> citys = GoodInfoActivity.this.getProvinceCity().get(province.getId());
                for (City city : citys) {
                    toCityAdapter.add(city);
                }
                toCityAdapter.notifyDataSetChanged();
                // toCity.setSelection(0);
            }
            
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                
            }
        });
        
    }
    
    private void fillDate() {
        // TODO Auto-generated method stub
        
        List<Province> provinces = new ArrayList<Province>();
        provinces.add(new Province(getResources().getString(R.string.any)));
        provinces.addAll(getProvinces());
        ArrayAdapter<Province> fromProvinceAdapter = new ArrayAdapter<Province>(this, android.R.layout.simple_spinner_item, provinces);
        fromProvinceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        fromProvince.setAdapter(fromProvinceAdapter);
        fromProvince.setSelection(this.getProvinceSpiner().getSelectedItemPosition() + 1);
        fromProvinceAdapter.notifyDataSetChanged();
        List<City> fromCitys = new ArrayList<City>();
        fromCitys.addAll(this.getCitys());
        fromCityAdapter = new ArrayAdapter<City>(this, android.R.layout.simple_spinner_item, fromCitys);
        fromCityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        fromCity.setAdapter(fromCityAdapter);
        fromCity.setSelection(this.getCitySpiner().getSelectedItemPosition());
        fromCityAdapter.notifyDataSetChanged();
        
        ArrayAdapter<Province> toProvinceAdapter = new ArrayAdapter<Province>(this, android.R.layout.simple_spinner_item, provinces);
        toProvinceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        toProvince.setAdapter(toProvinceAdapter);
        toProvince.setSelection(this.getProvinceSpiner().getSelectedItemPosition() + 1);
        toProvinceAdapter.notifyDataSetChanged();
        
        List<City> toCitys = new ArrayList<City>();
        toCitys.addAll(this.getCitys());
        toCityAdapter = new ArrayAdapter<City>(this, android.R.layout.simple_spinner_item, toCitys);
        toCityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        toCity.setAdapter(toCityAdapter);
        toCity.setSelection(this.getCitySpiner().getSelectedItemPosition());
        toCityAdapter.notifyDataSetChanged();
        
    }
    
    private void initComponent() {
        // TODO Auto-generated method stub
        fromProvince = (Spinner) this.findViewById(R.id.fromProvince);
        fromCity = (Spinner) this.findViewById(R.id.fromCity);
        toProvince = (Spinner) this.findViewById(R.id.toProvince);
        toCity = (Spinner) this.findViewById(R.id.toCity);
    }
    
    protected void changeSearchPanelVisibility() {
        // TODO Auto-generated method stub
        LinearLayout searchPanel = (LinearLayout) findViewById(R.id.searchPanel);
        if (View.VISIBLE == searchPanel.getVisibility()) {
            searchPanel.setVisibility(View.GONE);
            btnExpand.setText(R.string.expand);
        } else {
            searchPanel.setVisibility(View.VISIBLE);
            btnExpand.setText(R.string.packup);
        }
    }
    
    private void focusProvince() {
        Car car = LocationUploadService.getInstance().getCar();
        for (int i = 0; i < provinces.size(); i++) {
            Province province = provinces.get(i);
            if (province.getId() == car.getProvince()) {
                provinceSpiner.setSelection(i);
                break;
            }
        }
    }
    
    private void focusCity() {
        Car car = LocationUploadService.getInstance().getCar();
        citys.clear();
        citys.add(new City(getResources().getString(R.string.any)));
        citys.addAll(provinceCity.get(car.getProvince()));
        cityAdapter.notifyDataSetChanged();
        for (int i = 0; i < citys.size(); i++) {
            City city = citys.get(i);
            if (city.getId() == car.getCityId()) {
                citySpiner.setSelection(i);
                break;
            }
        }
    }
    
    private void fillProvinceSpinner() {
        
        ArrayAdapter<Province> provinceAdapter = new ArrayAdapter<Province>(this, android.R.layout.simple_spinner_item, provinces);
        provinceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        provinceSpiner.setAdapter(provinceAdapter);
        provinceAdapter.notifyDataSetChanged();
        provinceSpiner.setOnItemSelectedListener(new OnItemSelectedListener() {
            
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                Log.i(tag, "select province " + provinces.get(position));
                citys.clear();
                citys.add(new City(getResources().getString(R.string.any)));
                citys.addAll(provinceCity.get(provinces.get(position).getId()));
                cityAdapter.notifyDataSetChanged();
                // citySpiner.setSelection(0);
                goodInfoAdapter.removeAll();
            }
            
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                
            }
        });
        
    }
    
    private void fillCitySpinner() {
        cityAdapter = new ArrayAdapter<City>(this, android.R.layout.simple_spinner_item, citys);
        cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        citySpiner.setAdapter(cityAdapter);
        cityAdapter.notifyDataSetChanged();
        citySpiner.setOnItemSelectedListener(new OnItemSelectedListener() {
            
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                City city = citys.get(position);
                Log.i(tag, "select city " + city);
                if (city.getId() != null) {
                    MainTabActivity.service.write(CommandFactory.getGoods(city.getId()));
                    listView.setOnScrollListener(null);
                    goodInfoAdapter.removeAll();
                }
                MainTabActivity.service.getCar().setFocusCity(city.getId());
            }
            
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                
            }
        });
    }
    
    public Spinner getProvinceSpiner() {
        return provinceSpiner;
    }
    
    public void setProvinceSpiner(Spinner provinceSpiner) {
        this.provinceSpiner = provinceSpiner;
    }
    
    public Spinner getCitySpiner() {
        return citySpiner;
    }
    
    public void setCitySpiner(Spinner citySpiner) {
        this.citySpiner = citySpiner;
    }
    
    public GoodInfoItemAdapter getGoodInfoAdapter() {
        return goodInfoAdapter;
    }
    
    public void setGoodInfoAdapter(GoodInfoItemAdapter goodInfoAdapter) {
        this.goodInfoAdapter = goodInfoAdapter;
    }
    
    public AlertDialog getMenuDialog() {
        return menuDialog;
    }
    
    public int[] getMenu_name_array() {
        return menu_name_array;
    }
    
    public ListView getListView() {
        return listView;
    }
    
    public ListView getSearchListView() {
        return searchListView;
    }
    
    public AlertDialog getSearchDialog() {
        return searchDialog;
    }
    
    public void setSearchDialog(AlertDialog searchDialog) {
        this.searchDialog = searchDialog;
    }
    
    public List<Province> getProvinces() {
        return provinces;
    }
    
    public List<City> getCitys() {
        return citys;
    }
    
    public Hashtable<Integer, List<City>> getProvinceCity() {
        return provinceCity;
    }
    
    public SearchObject getSo() {
        return so;
    }
    
    public void setSo(SearchObject so) {
        this.so = so;
    }
    
    public View getFooterView() {
        return footerView;
    }
    
    public View getSearchInfoView() {
        return searchInfoView;
    }
    
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.e(tag, "scheduledExecutorService is shutdown");
        scheduledExecutorService.shutdown();
    }
    
    public GoodInfoItemAdapter getSearchGoodInfoAdapter() {
        return searchGoodInfoAdapter;
    }
    
    private GoodInfo wrapGoodInfo(JSONObject o) throws JSONException {
        GoodInfo goodInfo = new GoodInfo();
        goodInfo.setContact(o.getString("contact"));
        goodInfo.setMsg(o.getString("txt"));
        goodInfo.setIndex(o.getInt("index"));
        goodInfo.setAddTime(o.getString("addTime"));
        return goodInfo;
    }
}
